﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using ProjectModule;

namespace TableModule
{
    [XmlInclude(typeof(TableData))]
    public class TableData
    {
        public TableSingleData TableSingleData;
        
        public TableData()
        {
            TableSingleData = new TableSingleData();
        }
        public static TableData Load()
        {
            TableData pDoc=null;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TableData));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(ProjectManager.ProjectPath + @"/TableData/TableData" + ".xml");
                pDoc = (TableData)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                
                pDoc.TableSingleData.dicTableDataLocal = pDoc.TableSingleData.listTableDataLocal.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataPos = pDoc.TableSingleData.listTableDataPos.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataSpeed = pDoc.TableSingleData.listTableDataSpeed.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataTool = pDoc.TableSingleData.listTableDataTool.ToDictionary(p => p.strName);
               
            }
            catch(IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new TableData();
            }
            catch (Exception)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                //MessageBox.Show(eMy.ToString() + "即将打开备份文件", ProjectManager.ProjectPath + @"/TableData/TableData" + ".xml");

                fsReader = File.OpenRead(ProjectManager.ProjectPath + @"/TableData/TableData" + ".xml.bak");
                pDoc = (TableData)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.TableSingleData.dicTableDataLocal = pDoc.TableSingleData.listTableDataLocal.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataPos = pDoc.TableSingleData.listTableDataPos.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataSpeed = pDoc.TableSingleData.listTableDataSpeed.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataTool = pDoc.TableSingleData.listTableDataTool.ToDictionary(p => p.strName);
                pDoc.Save();
            }
            return pDoc;
        }
        public static TableData LoadGlabal()
        {
            TableData pDoc=null;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TableData));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/TableData/TableData" + ".xml");
                pDoc = (TableData)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.TableSingleData.dicTableDataLocal = pDoc.TableSingleData.listTableDataLocal.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataPos = pDoc.TableSingleData.listTableDataPos.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataSpeed = pDoc.TableSingleData.listTableDataSpeed.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataTool = pDoc.TableSingleData.listTableDataTool.ToDictionary(p => p.strName);
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new TableData();
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                MessageBox.Show(eMy.ToString() + "即将打开备份文件", Application.StartupPath + @"/TableData/TableData" + ".xml");

                fsReader = File.OpenRead(Application.StartupPath + @"/TableData/TableData" + ".xml.bak");
                pDoc = (TableData)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.TableSingleData.dicTableDataLocal = pDoc.TableSingleData.listTableDataLocal.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataPos = pDoc.TableSingleData.listTableDataPos.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataSpeed = pDoc.TableSingleData.listTableDataSpeed.ToDictionary(p => p.strName);
                pDoc.TableSingleData.dicTableDataTool = pDoc.TableSingleData.listTableDataTool.ToDictionary(p => p.strName);
                pDoc.SaveGlabal();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(ProjectManager.ProjectPath + @"/TableData/"))
            {
                Directory.CreateDirectory(ProjectManager.ProjectPath + @"/TableData/");
            }
            FileStream fsWriter = new FileStream(ProjectManager.ProjectPath + "/TableData/TableData" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TableData));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(ProjectManager.ProjectPath + "/TableData/TableData" + ".xml", ProjectManager.ProjectPath + "/TableData/TableData" + ".xml.bak", true);
            

            return true;
        }
        public bool SaveGlabal()
        {
            if (!Directory.Exists(Application.StartupPath + @"/TableData/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/TableData/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + "/TableData/TableData" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TableData));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(Application.StartupPath + "/TableData/TableData" + ".xml", Application.StartupPath + "/TableData/TableData" + ".xml.bak", true);
           


            return true;
        }
    }
}
